package com.co.controller;

import java.util.HashMap;

import javax.swing.JFrame;

import com.co.utils.Constants;
import com.co.utils.JPage;
import com.co.view.IndexPage;
import com.co.view.LoginPage;
import com.co.view.MainMenuPage;
import com.co.view.ObraSocialPage;

public class ViewManager {

	private static ViewManager viewManager;

	private HashMap pageInstances = null;
	private String currentPageId = null;

	public ViewManager() {
		pageInstances = new HashMap();
	}

	public static ViewManager getInstance() {
		if (viewManager == null) {
			viewManager = new ViewManager();
		}
		return viewManager;
	}

	public void suscriptPage(String pageId, JFrame pageValue) {
		pageInstances.put(pageId, pageValue);
	}

	public void setCurrentPageId(String pageId) {
		this.currentPageId = pageId;
	}

	public String getCurrentPageId() {
		return this.currentPageId;
	}

	public void goToPage(String nextPageId) {

		JPage nextPage = this.getPage(nextPageId);
		nextPage.setVisible(true);

		if (getCurrentPageId() != null) {
			JPage currentPage = (JPage) pageInstances.get(currentPageId);
			currentPage.setVisible(false);
		}

		setCurrentPageId(nextPageId);
	}

	private JPage getPage(String pageId) {

		if ((JPage) pageInstances.get(pageId) != null)
			return (JPage) pageInstances.get(pageId);
		else
			return createPage(pageId);
	}

	private JPage createPage(String pageId) {
		if (pageId.equals(Constants.INDEX_PAGE)) {
			return IndexPage.getInstance();
		}
		if (pageId.equals(Constants.LOGIN_PAGE)) {
			return LoginPage.getInstance();
		}
		if (pageId.equals(Constants.MAIN_MENU_PAGE)) {
			return MainMenuPage.getInstance();
		}
		if (pageId.equals(Constants.OBRA_SOCIAL_PAGE)) {
			return ObraSocialPage.getInstance();
		}
		// if (pageId.equals(Constants.CUSTOMERS_PAGE)) {
		//
		// }
		// if (pageId.equals(Constants.CUSTOMERS_CREATE_PAGE)) {
		//
		// }
		// if (pageId.equals(Constants.CUSTOMERS_SEARCH_PAGE)) {
		//
		// }
		return null;
	}

}